import { Controller, Get, Post, Body, Patch, Param, Delete, Inject, HttpStatus } from '@nestjs/common';
import { UserService } from './user.service';
import { UserLoginDto } from './dto/user-login.dto';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { ApiBasicAuth, ApiBearerAuth, ApiBody, ApiCookieAuth, ApiExcludeController, ApiExcludeEndpoint, ApiOperation, ApiQuery, ApiResponse } from '@nestjs/swagger';

@Controller('user')
// @ApiExcludeController() 隐藏整个控制器api
export class UserController {
  constructor(private readonly userService: UserService) { }
  @Inject(JwtService) private jwtService: JwtService

  @Post('login')
  //@ApiExcludeEndpoint() 隐藏单个路径api
  @ApiOperation({ summary: '用户登录',description: '登录描述' })
  async login(@Body() userlogin: UserLoginDto) {
    const user = await this.userService.login(userlogin);
    
    const token = this.jwtService.sign({
      user: {
        username:user.username,
        roles: user.roles
      }
    });

    return { token }

  }

  @Get('init')
  @ApiBearerAuth()
  @ApiCookieAuth()
  @ApiBasicAuth()
  @ApiResponse({
    status: HttpStatus.OK,
    description: 'aaa 成功',
    type: String
  })
  @ApiQuery({
    name: 'a1',
    type: String,
    description: 'a1 param',
    required: false,
    example: '1111',
})
@ApiResponse({
  status: HttpStatus.UNAUTHORIZED,
  description: 'id 不合法'
})
  async initData() {
    await this.userService.initData();
    return "Data Initialized";
  }


}
